package com.health.viewscontrols.medical.device.bg;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.clj.fastble.utils.HexUtil;
import com.health.tablebean.BTDevice;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.medical.base.BaseDeviceActivity;
import com.nova.health.ble.callback.OnDeviceConnectChangedListener;
import com.novabluetooth.data.DeviceShowSaveData;
import com.novabluetooth.views.BlueToothConstant;

import java.util.List;
import java.util.UUID;

/**
 * 三诺血糖仪
 */
public class BGSannuoActivity extends BaseDeviceActivity {

    public final static UUID UUID_SERVICE =
            UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");//蓝牙设备的Service的UUID --0000ffe0
    public final static UUID UUID_NOTIFY =
            UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");//蓝牙设备的Characteristic的UUID --0000ffe1


    @Override
    public void doBusiness() {
        tv_operation_tips.setText(com.nova.health.ble.R.string.bg_sannuo);
        mBleDevice.setDevice_Service_UUID(UUID_SERVICE);
        mBleDevice.setDevice_Notify_UUID(UUID_NOTIFY);
        mBleDevice.setConnectChangedListener(new OnDeviceConnectChangedListener() {
            @Override
            public void onConnected() {
                Log.e(TAG, "onConnected: ");
                isConnected = true;
                runOnUiThread(() -> {
                    blueToothState.setText("蓝牙已连接");
                    showProgressDialog("等待接收数据" + deviceEnum.getTitle() + "数据");
                });
            }

            @Override
            public void onDisconnected() {
                isConnected = false;
                Log.e(TAG, "onDisconnected: ");
                cancelProgressDialog();
                runOnUiThread(() -> {
                    blueToothState.setText("蓝牙连接失败");
                });
            }
        });
    }

    @Override
    public void startDevice() {
        if (isConnected) {
            blueToothState.setText("蓝牙已连接");
            showProgressDialog("等待接收数据" + deviceEnum.getTitle() + "数据");
        } else {
            blueToothState.setText("蓝牙开始连接");
            showProgressDialog("正在连接" +  deviceEnum.getTitle());
            mBleDevice.setConnect(true);
            mBleDevice.setMAC(deviceEnum.getMac());
            mBleDevice.connect(deviceEnum.getMac());
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mBleDevice.setConnect(true);
                    mBleDevice.setMAC(deviceEnum.getMac());
                    mBleDevice.connect(deviceEnum.getMac());
                }
            },3200);
        }
    }

    @Override
    public void update(String hexStr) {
        Log.e(TAG, "update: " + hexStr );
        try {
            byte[] data = HexUtil.hexStringToBytes(hexStr);
            Log.e(TAG, "update: " + data.length );
            if (data.length > 12) {
                String resultStr = "" + (data[11] & 0xFF) + (data[12] & 0xff);
                float bg = (float) (Integer.parseInt(resultStr) / 10.0);
                Log.e(TAG, "onCharacteristicChanged: " + bg  + "mmol/L");
                tv_state.setText("获取测量结果成功");
                tv_result.setText("血糖值： " + bg  + "mmol/L");
                DeviceShowSaveData.getDataShow(bg + "","Sinocare");
                DeviceShowSaveData.saveXuetang(bg + "");
                disconnectDevice();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void disconnectDevice() {
        try {
            cancelProgressDialog();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            mBleDevice.setConnect(false);
            mBleDevice.disconnect();
            mBleDevice.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
